//
//  USSearchHistoryMenu.m
//  usale0001
//
//  Created by 余国平 on 2016/12/8.
//  Copyright © 2016年 YU. All rights reserved.
//

#import "USSearchHistoryMenu.h"

#define CellLineEdgeInsets UIEdgeInsetsMake(0, 0, 0, 0)

@interface USSearchHistoryMenu() <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, assign) CGPoint origin;
@property (nonatomic, assign) CGFloat rowHeight;
@end

@implementation USSearchHistoryMenu

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

//CGRectMake(0.0333 * kUSScreenWidth, 0.0187 * kUSScreenHeight, kUSScreenWidth - 0.0666 * kUSScreenWidth, kSearchFieldHeight)

- (instancetype)initWithDataArr:(NSArray *)dataArr {
    self = [super initWithFrame:CGRectMake(0, 0.0187 * kUSScreenHeight + kSearchFieldHeight, kUSScreenWidth, kUSScreenHeight - kSearchFieldHeight- 0.0187 * kUSScreenHeight)];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.origin = CGPointMake(0.0333 * kUSScreenWidth, 0);
        self.rowHeight = 40;
        self.arrData = [dataArr copy];
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(_origin.x, _origin.y + 3, kUSScreenWidth - 0.0666 * kUSScreenWidth, _rowHeight * (dataArr.count + 1)) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [self addSubview:_tableView];
        _tableView.backgroundColor = kUSColorGray;
        _tableView.alpha = 0.9;
        _tableView.layer.cornerRadius = 2;
        _tableView.bounces = NO;
        _tableView.separatorColor = kUSColorFromRGB(0x62B4F2);
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"USSearchHistoryMenu"];
        if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [self.tableView setSeparatorInset:CellLineEdgeInsets];
        }
        
        if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [self.tableView setLayoutMargins:CellLineEdgeInsets];
        }
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrData.count + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.rowHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"USSearchHistoryMenu"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.frame = CGRectMake(40, 10, 200, 20);
    
    if (self.arrData.count == indexPath.row) {
        cell.textLabel.text = USLocalizedString(@"清除记录");
        cell.textLabel.textColor =  [UIColor redColor];
    } else {
        cell.textLabel.text = self.arrData[indexPath.row];
        cell.textLabel.textColor = kUSColorFieldText;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if([self.delegate respondsToSelector:@selector(usSearchHistoryMenu:didSelectRowAtIndexPath:)]){
        [self.delegate usSearchHistoryMenu:tableView didSelectRowAtIndexPath:indexPath];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (!self.NotDissmiss) {
        [self dismissWithCompletion:nil];
        
    }else{
        if (self.dismiss) {
            self.dismiss();
        }
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:CellLineEdgeInsets];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:CellLineEdgeInsets];
    }
}
- (void)dismissWithCompletion:(void (^)(USSearchHistoryMenu *object))completion
{
    
    __weak __typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.alpha = 0;
        weakSelf.tableView.frame = CGRectMake(weakSelf.origin.x - kUSMargin, weakSelf.origin.y + 3, 0, 0);
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
        if (completion) {
            completion(weakSelf);
        }
        if (weakSelf.dismiss) {
            weakSelf.dismiss();
        }
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if (![touch.view isEqual:self.tableView]) {
        [self dismissWithCompletion:nil];
    }
}


@end
